﻿#region License
/*Copyright (c) 2010 Andrew Collard (andrewcollard@hotmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#endregion

using System.IO;
using System.Text;

namespace TorchView.Game
{
  public static class SVTHelper
  {
    /// <summary>
    /// Helper method used to seek ahead in the underlying stream
    /// </summary>
    /// <param name="reader">The binary reader which contains the stream</param>
    /// <param name="count">Number of bytes to seek forward by</param>
    public static void SeekForward(BinaryReader reader, int count)
    {
      reader.BaseStream.Seek(count, SeekOrigin.Current);
    }

    /// <summary>
    /// Helper method used to seek back in the underlying stream
    /// </summary>
    /// <param name="reader">The binary reader which contains the stream</param>
    /// <param name="count">Number of bytes to seek backward by</param>
    public static void SeekBackward(BinaryReader reader, int count)
    {
      reader.BaseStream.Seek(-count, SeekOrigin.Current);
    }

    /// <summary>
    /// Scan the stream until we find a specific character repeated a specified number of times
    /// </summary>
    /// <param name="reader">The reader which contains the stream</param>
    /// <param name="match">The byte we are looking to match</param>
    /// <param name="count">The number of consecutive bytes we want</param>
    /// <returns>true if we found a match, false if we reached the end of the file without finding a match</returns>
    public static bool ScanUntil(BinaryReader reader, byte match, int count)
    {
      int c = 0;
      bool found = false;
      while (reader.BaseStream.Position < reader.BaseStream.Length)
      {
        var value = reader.ReadByte();
        if (value == match)
        {
          c++;
          if (c == count)
            found = true;
        }
        else if (found)
        {
          reader.BaseStream.Seek(-count - 1, SeekOrigin.Current);
          return true;
        }
        else
          c = 0;
      }
      return false;
    }

    /// <summary>
    /// Read a string from the stream
    /// </summary>
    /// <param name="reader">Reader to read the string from</param>
    /// <returns>The string found at the current position in the stream</returns>
    public static string ReadString(BinaryReader reader)
    {
      short num2 = reader.ReadInt16();
      if (num2 < 1)
        return string.Empty;
      return Encoding.Unicode.GetString(reader.ReadBytes(num2 * 2));
    }

    /// <summary>
    /// Read a string from the stream
    /// </summary>
    /// <param name="reader">Reader to read the string from</param>
    /// <returns>The string found at the current position in the stream</returns>
    public static string ReadString32(BinaryReader reader)
    {
        int num2 = reader.ReadInt32();
        if (num2 < 1)
            return string.Empty;
        return Encoding.Unicode.GetString(reader.ReadBytes(num2 * 2));
    }

    /// <summary>
    /// Write a string to the writer
    /// </summary>
    /// <param name="writer">Writer to write the string to</param>
    /// <param name="value">The string to write to the stream</param>
    public static void WriteString(BinaryWriter writer, string value)
    {
      writer.Write((ushort)value.Length);
      if (value.Length > 0)
        writer.Write(Encoding.Unicode.GetBytes(value));
    }
  }
}
